package com.jiufen.Controller;


import cn.hutool.core.lang.Console;
import com.jiufen.Exception.RemainingStudentsException;
import com.jiufen.Pojo.Dept;
import com.jiufen.Pojo.Result;
import com.jiufen.Service.DeptService;
import com.jiufen.anno.LogOperate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * dept controller
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result queryDeptAll(){
//        log.info("查询depts，time："+LocalDateTime.now());
        log.info("查询depts，time：{}",LocalDateTime.now());
        List<Dept> depts = deptService.queryDeptAll();
        return Result.success(depts);
    }

    /**
     * 根据ID删除部门
     * @return
     */
   /* @DeleteMapping
    public Result deleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        Console.log("删除部门，ID："+id);
        int i = deptService.deleteById(Integer.valueOf(id));
        if(i==0){
            Console.error("删除失败");
        }
        return Result.success();
    }*/
    /**
     * 根据ID删除部门
     * @return
     */
    @LogOperate
    @DeleteMapping
    public Result deleteById(Integer id) throws RemainingStudentsException {
        log.info("删除部门，ID：{}",id);
        int i = deptService.deleteById(Integer.valueOf(id));
        if(i==0){
            Console.error("删除失败");
        }
        return Result.success();
    }


    /**
     * 添加部门信息（json参数）
     */
    @LogOperate
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门，dept：{}",dept);
        int i = deptService.addDept(dept);
        if(i==0){
            Console.error("添加失败");
        }
        return Result.success();
    }

    /**
     * 根据ID查询部门信息  路径参数
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result queryByDeptId(@PathVariable Integer id){
        log.info("根据ID查询部门信息，ID：{}",id);
        Dept dept = deptService.queryByDeptId(id);
        log.info("根据ID查询部门信息，dept：{}",dept);
        return Result.success(dept);
    }

    /**
     * 根据ID修改部门信息
     */
    @LogOperate
    @PutMapping
    public Result updateDeptById(@RequestBody Dept dept){
        log.info("根据ID修改部门信息，dept：{}",dept);
        int i = deptService.updateDeptById(dept);
        if(i==0){
            log.error("修改失败");
        }
        return Result.success();
    }

}
